package com.erp.module.ProductionExecute.ProductionExecuteXx

import android.app.Activity
import android.app.Dialog
import android.content.Intent
import android.util.Log
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.GridLayoutManager
import com.erp.Interface.OnUserDataInterface
import com.erp.R
import com.erp.activity.BaseActivity
import com.erp.bean.ProductionExecuteBean
import com.erp.mclass.QuanXian
import com.erp.module.ProductionExecute.ProductionExecuteM
import com.erp.module.ProductionExecute.ProdutionExecuteContract
import com.erp.module.ProductionExecuter.FragmengtProductionExecute
import com.erp.myAdapter.ArrayAdapter
import com.erp.myAdapter.ProductionYhhAdapter
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_production_execute.*
import kotlinx.android.synthetic.main.dialog_inventory_supplier.*
import kotlinx.android.synthetic.main.header_knoya.*
import java.util.*


class ProductionExecuteActivity  : BaseActivity(), View.OnClickListener,ProdutionExecuteContract.ProductionActivityKV {
    var updatedialog: AlertDialog? = null
    lateinit var bean : ProductionExecuteBean
    lateinit var adapter: ProductionYhhAdapter
    lateinit var P : ProdutionExecuteContract.ProductionActivityKP
    lateinit var md:ProductionExecuteM
    private var code = 0

    override fun initCreate() {
        code = intent.getIntExtra("code", code)
        iv_header_left.visibility = View.VISIBLE
        iv_header_left.setOnClickListener(this)
        tv_header_title.text = "上料操作"
        md = ProductionExecuteM()
        P = productionExecuteActivityP(this, this)
        bean = FragmengtProductionExecute.bean
        Myutil.settv(bean.heap_number,TvHeapNumber)
        Myutil.settv(bean.status_name,TvStatusName)
        Myutil.settv(bean.workshop,TvWorkshop)
        Myutil.settv(bean.elevator_number,TvElevatorNumber)
        Myutil.settv(bean.count,TvCount)
        Myutil.settv(bean.stock_name,TvStock)
        val layoutManager = GridLayoutManager(this, 1)
        adapter = ProductionYhhAdapter(this,bean.listyyh,this)
        LvYhhData.layoutManager = layoutManager
        LvYhhData.adapter = adapter
        if(bean.status == "2"){
            if(bean.stock == "0"){
                if(QuanXian.isProductionDispatchExecute){
                    BtBeiliao.visibility = View.VISIBLE
                }
            }else if(bean.stock == "1"){
                if(QuanXian.isProductionDispatchAccomplish){
                    BtQingqiuSongliao.visibility = View.VISIBLE
                }
                if(QuanXian.isProductionDispatchExecute){
                    BtQuxiaoBeiliao.visibility = View.VISIBLE
                }
            }
        }else if(bean.status == "3"){
            if(QuanXian.isProductionDispatchExecute){
                BtKaishiSongliao.visibility = View.VISIBLE
            }
            if(QuanXian.isProductionDispatchAccomplish){
                BtQuxiaoQingqiu.visibility = View.VISIBLE
            }
        }else if(bean.status == "4"){
            if(QuanXian.isProductionDispatchAccomplish){
                BtQuerenShouliao.visibility = View.VISIBLE
            }
            if(QuanXian.isProductionDispatchExecute){
                BtQuxiaoSongliao.visibility = View.VISIBLE
            }
        }
        BtBeiliao.setOnClickListener(this)
        BtQingqiuSongliao.setOnClickListener(this)
        BtKaishiSongliao.setOnClickListener(this)
        BtQuxiaoQingqiu.setOnClickListener(this)
        BtQuxiaoSongliao.setOnClickListener(this)
        BtQuerenShouliao.setOnClickListener(this)
        BtQuxiaoBeiliao.setOnClickListener(this)
        if(code == 1){
            if(bean.stock == "0"){
                wanchengbeiliao()
            }
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_production_execute
    }

    override fun onClick(v: View?) {
        when(v){
            iv_header_left -> {
                finish()
            }
            BtKaishiSongliao -> {
                val dialog = AlertDialog.Builder(this)
                dialog.setTitle("提示")
                dialog.setMessage("确定要开始送料吗？")
                dialog.setPositiveButton("确定") { dialog, which ->
                    updatedialog = Myutil.showupdatedialog(this, "正在执行", false)
                    md.ProductionSongliao(this,bean.plmd_id,"execute_accomplish",object : OnUserDataInterface{
                        override fun onNot(throwable: String?) {
                            updatedialog?.dismiss()
                        }

                        override fun onUserData(data: String?) {
                            updatedialog?.dismiss()
                            var intent = Intent()
                            setResult(Activity.RESULT_OK, intent)
                            finish()
                        }

                    })
                }
                dialog.setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
                dialog.show()

            }
            BtQuxiaoSongliao -> {
                val dialog = AlertDialog.Builder(this)
                dialog.setTitle("提示")
                dialog.setMessage("确定要取消送料吗？")
                dialog.setPositiveButton("确定") { dialog, which ->
                    updatedialog = Myutil.showupdatedialog(this, "正在执行", false)
                    md.ProductionSongliao(this,bean.plmd_id,"cancel_delivery",object : OnUserDataInterface{
                        override fun onNot(throwable: String?) {
                            updatedialog?.dismiss()
                        }

                        override fun onUserData(data: String?) {
                            updatedialog?.dismiss()
                            var intent = Intent()
                            setResult(Activity.RESULT_OK, intent)
                            finish()
                        }

                    })
                }
                dialog.setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
                dialog.show()

            }
            BtQuxiaoBeiliao -> {
                val dialog = AlertDialog.Builder(this)
                dialog.setTitle("提示")
                dialog.setMessage("确定取消备料吗？")
                dialog.setPositiveButton("确定") { dialog, which ->
                    updatedialog = Myutil.showupdatedialog(this, "正在执行", false)
                    md.ProductionSongliao(this,bean.plmd_id,"cancel_stock_accomplish",object : OnUserDataInterface{
                        override fun onNot(throwable: String?) {
                            updatedialog?.dismiss()
                        }

                        override fun onUserData(data: String?) {
                            updatedialog?.dismiss()
                            var intent = Intent()
                            setResult(Activity.RESULT_OK, intent)
                            finish()
                        }

                    })
                }
                dialog.setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
                dialog.show()
            }
            BtBeiliao ->{
                wanchengbeiliao()

            }
            BtQingqiuSongliao -> {
                val dialog = AlertDialog.Builder(this)
                dialog.setTitle("提示")
                dialog.setMessage("确定要请求送料吗？")
                dialog.setPositiveButton("确定") { dialog, which ->
                    updatedialog = Myutil.showupdatedialog(this, "正在执行", false)
                    md.ProductionShouLiao(this,bean.plmd_id,"execute_on",object : OnUserDataInterface{
                        override fun onNot(throwable: String?) {
                            updatedialog?.dismiss()
                        }

                        override fun onUserData(data: String?) {
                            updatedialog?.dismiss()
                            var intent = Intent()
                            setResult(Activity.RESULT_OK, intent)
                            finish()
                        }

                    })
                }
                dialog.setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
                dialog.show()

            }
            BtQuxiaoQingqiu -> {
                val dialog = AlertDialog.Builder(this)
                dialog.setTitle("提示")
                dialog.setMessage("确定要取消请求送料吗？")
                dialog.setPositiveButton("确定") { dialog, which ->
                    updatedialog = Myutil.showupdatedialog(this, "正在执行", false)
                    md.ProductionShouLiao(this,bean.plmd_id,"cancel_request",object : OnUserDataInterface{
                        override fun onNot(throwable: String?) {
                            updatedialog?.dismiss()
                        }

                        override fun onUserData(data: String?) {
                            updatedialog?.dismiss()
                            var intent = Intent()
                            setResult(Activity.RESULT_OK, intent)
                            finish()
                        }

                    })
                }
                dialog.setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
                dialog.show()

            }
            BtQuerenShouliao -> {
                val dialog = AlertDialog.Builder(this)
                dialog.setTitle("提示")
                dialog.setMessage("确定收料？")
                dialog.setPositiveButton("确定") { dialog, which ->
                    updatedialog = Myutil.showupdatedialog(this, "正在执行", false)
                    md.ProductionWancheng(this,bean.plmd_id,object : OnUserDataInterface{
                        override fun onNot(throwable: String?) {

                        }

                        override fun onUserData(data: String?) {
                            var intent = Intent()
                            setResult(Activity.RESULT_OK, intent)
                            finish()
                        }

                    })
                }
                dialog.setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
                dialog.show()

            }
//            iunameadialog.AtInventorySupplier -> {
//                iunameadialog.getWindow()
//                        .clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
//
//            }
        }
    }

    fun  wanchengbeiliao(){
        val dialog = AlertDialog.Builder(this)
        dialog.setTitle("提示")
        dialog.setMessage("确定完成备料吗？")
        dialog.setPositiveButton("确定") { dialog, which ->
            updatedialog = Myutil.showupdatedialog(this, "正在执行", false)
            md.ProductionSongliao(this,bean.plmd_id,"stock_accomplish",object : OnUserDataInterface{
                override fun onNot(throwable: String?) {
                    updatedialog?.dismiss()
                }

                override fun onUserData(data: String?) {
                    updatedialog?.dismiss()
                    var intent = Intent()
                    setResult(Activity.RESULT_OK, intent)
                    finish()
                }

            })
        }
        dialog.setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
        dialog.show()
    }

    override fun inventory_supplier_dialog(bean:ProductionExecuteBean.plmd_yhhbean) {
        yhhbean = bean
        data_inventory_supplier_dialog()
    }
    override fun getiu_names(ids: ArrayList<String>, names: ArrayList<String>) {
        this.iuIds = ids;
        this.iuNames = names;
        val av = ArrayAdapter(this@ProductionExecuteActivity, android.R.layout.simple_dropdown_item_1line, names)
        iunameadialog.AtInventorySupplier.setAdapter(av)
        iunameadialog.AtInventorySupplier.setText(yhhbean.getiu_name())
        Log.d("plmdactivity","ss"+names)
    }

    override fun gongyingxiugaiok() {
        yhhbean.iu_name2 = iuname;
    }

    lateinit var iunameadialog: Dialog
    lateinit var yhhbean:ProductionExecuteBean.plmd_yhhbean
    var iuIds: ArrayList<String>  = ArrayList<String>()
    var iuNames: ArrayList<String>  = ArrayList<String>()
    var iuname :String = ""
    private fun data_inventory_supplier_dialog() {
        iunameadialog = AlertDialog.Builder(this).create()
        iunameadialog.show()
        iunameadialog.window?.setContentView(R.layout.dialog_inventory_supplier)
        if(iuIds.size == 0 && iuNames.size == 0){
            P.getIu()
        }else{
            val av = ArrayAdapter(this@ProductionExecuteActivity, android.R.layout.simple_dropdown_item_1line, iuNames)
            iunameadialog.AtInventorySupplier.setAdapter(av)
            iunameadialog.AtInventorySupplier.setText(yhhbean.getiu_name())
        }
        iunameadialog.AtInventorySupplier.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                iunameadialog.getWindow()?.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
                iunameadialog.AtInventorySupplier.showDropDown()
            }
        })
//        iunameadialog.AtInventorySupplier -> {
//            iunameadialog.getWindow()
//                    .clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
//
//        }
//        iunameadialog.AtInventorySupplier.setOnClickListener(this)

        iunameadialog.LeftBt.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                iunameadialog.dismiss()
            }
        })
        iunameadialog.RightBt.setOnClickListener(object :View.OnClickListener{
            override fun onClick(v: View?) {
                if (!iuNames.contains(iunameadialog.AtInventorySupplier.text.toString())) {
                    Myutil.MyTost("输入的供应商不存在,不能上传")
                    return
                }
                var iu_id = "";
                iuNames.indices
                        .filter { i -> iuNames[i] == iunameadialog.AtInventorySupplier.text.toString() }
                        .forEach { iu_id = iuIds[it] }
                P.setIu(yhhbean.yhh_id,iu_id)
                iuname = iunameadialog.AtInventorySupplier.text.toString();
                iunameadialog.dismiss()
            }

        })
    }

}